<?php

class treeComponents extends sfComponents
{
    public function executeCulture()
    {
        $returnUrl = $this->getModuleName() . '/' . $this->getActionName();
        if( ($id = (int)$this->getRequestParameter('id', 0)) )
        {
            $returnUrl .= '?id=' . $id;
        }
        // TODO: move to system module
        $this->setFlash('returnUrl',$returnUrl);
    }
   public function executeMain_menu()
    {
        $arTree = Tree::getTreeByLabel(MyConstants::IA, sfContext::getInstance()->getUser()->getCulture());
        $this->arTree = $arTree[0]['children'];
        
    }
    public function executeLast_posts()
    {
        $id = BlogUserInfo::getInstance()->getUser()->getId();
        $nick = BlogUserInfo::getInstance()->getUser()->getNick();
        $c = new Criteria();
        $c->add(BlogPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        if( $id > 0 )
        {
            $c->add(BlogPeer::USER_ID, $id);
        }else 
        {
            $c->add(BlogPeer::USER_ID, -1);
        }
        $c->addDescendingOrderByColumn(BlogPeer::CREATED_AT);
        $c->setLimit(sfConfig::get('app_posts_count', 10));
        $blogs = BlogPeer::doSelect($c);
        $this->blogs = array();
        $i = 0;
        foreach( $blogs as $blog )
        {
            $i++;
            $slug = Seo::getSlugByObj($blog->getId(), 'blog', 'show', $blog->getCulture());
            if($slug)
            {
                $this->blogs[$i]['obj'] = $blog;
                $this->blogs[$i]['params'] = 'nick=' . $nick . '&slug=' . $slug;
            }
        }
    }
    public function executeTags()
    {
        $id = BlogUserInfo::getInstance()->getUser()->getId();
        $nick = BlogUserInfo::getInstance()->getUser()->getNick();
        $c = new Criteria();
        $c->add(TagPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        if( $id > 0 )
        {
            $c->add(TagPeer::USER_ID, $id);
        }else 
        {
            $c->add(TagPeer::USER_ID, -1);
        }
        $c->addDescendingOrderByColumn(TagPeer::PRIORITY);
        $tags = TagPeer::doSelect($c);
        $this->tags = array();
        $i = 0;
        foreach( $tags as $tag )
        {
            $i++;
            $this->tags[$i]['obj'] = $tag;
            $this->tags[$i]['params'] = 'nick=' . $nick . '&tag=' . $tag->getSlugName();
        }
    }
    public function executeBranch()
    {
        $this->userCulture = $this->getUser()->getCulture();
        $arBranch = Tree::getTree($this->tree->getId(), $this->userCulture);

        // Root element
        $this->level = 1;
        if( $arBranch[0] )
        {
            $this->arBranch = $arBranch;
        }else
            $this->arBranch = array();
    }
    //for creating categories for products in tree
    public function executeBranch_separated()
    {
        $this->userCulture = $this->getUser()->getCulture();
        $tree = Tree::getTreeByLabel('PRODUCTS', $this->userCulture);

        // Root element
        $this->level = 1;
        if( $tree[0] )
        {
            $this->tree = $tree;
        }else
            $this->tree = array();
    }


    public function executeParent_branch()
    {
        $this->userCulture = $this->getUser()->getCulture();

        if( $objId = (int)$this->getRequestParameter('id', 0) )
        {
            $obj = Tree::getById($objId);
            $this->arParents = Tree::getBranchById($objId, $this->userCulture);
        }

    }
    
    public function executePath()
    {
        if( isset($this->label) && isset($this->path))
        {
            $text = Tree::getNameForPathByLabel($this->label, $this->path);
            $this->text = $text;
        }
        if( !isset($this->label) && isset($this->path))
        {
            $this->text = $this->path;
        }
        $this->userCulture = $this->getUser()->getCulture();
        if( $this->getRequestParameter('module') == 'tree' && $this->getRequestParameter('action') == 'page' )
        {
            if( $objId = (int)$this->getRequestParameter('id', 0) )
            {
                $obj = Tree::getById($objId);
                $this->arParents = Tree::getBranchById($objId, $this->userCulture);
                $this->obj = $obj;
            }
        }
    }
    
    public function executeGallery_list()
    {
        $c = new Criteria();
        $c->add(FilesPeer::OBJ_ID, $this->tree->getId());
        $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_TREE_PAGE);
        $c->addDescendingOrderByColumn(FilesPeer::PRIORITY);
        $this->arFiles = FilesPeer::doSelect($c);
        $this->totalFiles = count($this->arFiles);
    }

    public function executeTextblock()
    {
        $this->textblocks = Textblocks::getOneByObj($this->tree->getId(), MyConstants::TYPE_TREE_PAGE);
    }
}